#!/bin/bash

# VEPAI平台演示数据创建脚本
# 根据使用指南创建完整的示例数据

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

BASE_URL=${1:-"http://192.168.253.41:5000"}

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  VEPAI平台演示数据创建脚本${NC}"
echo -e "${BLUE}  API地址: ${BASE_URL}${NC}"
echo -e "${BLUE}========================================${NC}"

# 检查jq是否安装
if ! command -v jq &> /dev/null; then
    echo -e "${RED}错误: 需要安装jq工具来解析JSON响应${NC}"
    echo -e "${YELLOW}Ubuntu/Debian: sudo apt-get install jq${NC}"
    echo -e "${YELLOW}CentOS/RHEL: sudo yum install jq${NC}"
    echo -e "${YELLOW}MacOS: brew install jq${NC}"
    exit 1
fi

# 检查API是否可用
echo -e "${YELLOW}检查API连接...${NC}"
if ! curl -s "$BASE_URL/api/module" > /dev/null; then
    echo -e "${RED}错误: 无法连接到API服务 $BASE_URL${NC}"
    echo -e "${YELLOW}请确保VEPAI平台已启动: ./deploy.sh start${NC}"
    exit 1
fi

echo -e "${GREEN}✅ API连接正常${NC}"

# 1. 创建功能模块
echo -e "\n${YELLOW}1. 创建功能模块...${NC}"

echo -e "  📁 创建登录模块..."
MODULE_LOGIN_ID=$(curl -s -X POST "$BASE_URL/api/module" \
  -H "Content-Type: application/json" \
  -d '{"name": "登录模块", "description": "用户登录相关功能", "directory_name": "login"}' \
  | jq -r '.data.id')

echo -e "  📁 创建主菜单模块..."
MODULE_MENU_ID=$(curl -s -X POST "$BASE_URL/api/module" \
  -H "Content-Type: application/json" \
  -d '{"name": "主菜单模块", "description": "主界面菜单功能", "directory_name": "main_menu"}' \
  | jq -r '.data.id')

echo -e "  📁 创建设置模块..."
MODULE_SETTINGS_ID=$(curl -s -X POST "$BASE_URL/api/module" \
  -H "Content-Type: application/json" \
  -d '{"name": "设置模块", "description": "系统设置功能", "directory_name": "settings"}' \
  | jq -r '.data.id')

echo -e "${GREEN}✅ 模块创建完成 (登录:$MODULE_LOGIN_ID, 菜单:$MODULE_MENU_ID, 设置:$MODULE_SETTINGS_ID)${NC}"

# 2. 创建控件坐标
echo -e "\n${YELLOW}2. 创建控件坐标...${NC}"

echo -e "  📍 创建登录模块坐标..."
curl -s -X POST "$BASE_URL/api/coordinates/batch" \
  -H "Content-Type: application/json" \
  -d "[
    {\"element_name\": \"用户名输入框\", \"module_id\": $MODULE_LOGIN_ID, \"x_coord\": 100, \"y_coord\": 150, \"description\": \"用户名输入框坐标\"},
    {\"element_name\": \"密码输入框\", \"module_id\": $MODULE_LOGIN_ID, \"x_coord\": 100, \"y_coord\": 180, \"description\": \"密码输入框坐标\"}, 
    {\"element_name\": \"登录按钮\", \"module_id\": $MODULE_LOGIN_ID, \"x_coord\": 100, \"y_coord\": 220, \"description\": \"登录按钮坐标\"}
  ]" > /dev/null

echo -e "  📍 创建主菜单坐标..."
curl -s -X POST "$BASE_URL/api/coordinates/batch" \
  -H "Content-Type: application/json" \
  -d "[
    {\"element_name\": \"录像管理菜单\", \"module_id\": $MODULE_MENU_ID, \"x_coord\": 50, \"y_coord\": 100, \"description\": \"录像管理菜单按钮\"},
    {\"element_name\": \"设备管理菜单\", \"module_id\": $MODULE_MENU_ID, \"x_coord\": 50, \"y_coord\": 150, \"description\": \"设备管理菜单按钮\"},
    {\"element_name\": \"系统设置菜单\", \"module_id\": $MODULE_MENU_ID, \"x_coord\": 50, \"y_coord\": 200, \"description\": \"系统设置菜单按钮\"}
  ]" > /dev/null

echo -e "  📍 创建设置模块坐标..."
curl -s -X POST "$BASE_URL/api/coordinates/batch" \
  -H "Content-Type: application/json" \
  -d "[
    {\"element_name\": \"网络设置按钮\", \"module_id\": $MODULE_SETTINGS_ID, \"x_coord\": 200, \"y_coord\": 100, \"description\": \"网络设置按钮\"},
    {\"element_name\": \"保存按钮\", \"module_id\": $MODULE_SETTINGS_ID, \"x_coord\": 200, \"y_coord\": 300, \"description\": \"保存设置按钮\"}
  ]" > /dev/null

echo -e "${GREEN}✅ 坐标创建完成${NC}"

# 3. 创建基础步骤
echo -e "\n${YELLOW}3. 创建基础步骤...${NC}"

echo -e "  🔧 创建登录步骤..."
STEP_INPUT_USER=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"输入用户名\", \"step_type\": \"input\", \"module_id\": $MODULE_LOGIN_ID, \"coordinate_id\": 1, \"description\": \"在用户名输入框输入用户名\"}" \
  | jq -r '.data.id')

STEP_INPUT_PASS=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"输入密码\", \"step_type\": \"input\", \"module_id\": $MODULE_LOGIN_ID, \"coordinate_id\": 2, \"description\": \"在密码输入框输入密码\"}" \
  | jq -r '.data.id')

STEP_CLICK_LOGIN=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"点击登录按钮\", \"step_type\": \"click\", \"module_id\": $MODULE_LOGIN_ID, \"coordinate_id\": 3, \"description\": \"点击登录按钮\"}" \
  | jq -r '.data.id')

echo -e "  🔧 创建菜单操作步骤..."
STEP_CLICK_RECORD=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"点击录像管理\", \"step_type\": \"click\", \"module_id\": $MODULE_MENU_ID, \"coordinate_id\": 4, \"description\": \"点击录像管理菜单\"}" \
  | jq -r '.data.id')

STEP_CLICK_DEVICE=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"点击设备管理\", \"step_type\": \"click\", \"module_id\": $MODULE_MENU_ID, \"coordinate_id\": 5, \"description\": \"点击设备管理菜单\"}" \
  | jq -r '.data.id')

STEP_CLICK_SETTINGS=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"点击系统设置\", \"step_type\": \"click\", \"module_id\": $MODULE_MENU_ID, \"coordinate_id\": 6, \"description\": \"点击系统设置菜单\"}" \
  | jq -r '.data.id')

echo -e "  🔧 创建设置步骤..."
STEP_CLICK_NETWORK=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"点击网络设置\", \"step_type\": \"click\", \"module_id\": $MODULE_SETTINGS_ID, \"coordinate_id\": 7, \"description\": \"点击网络设置按钮\"}" \
  | jq -r '.data.id')

STEP_SAVE_SETTINGS=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"保存设置\", \"step_type\": \"click\", \"module_id\": $MODULE_SETTINGS_ID, \"coordinate_id\": 8, \"description\": \"点击保存设置按钮\"}" \
  | jq -r '.data.id')

echo -e "  🔧 创建前置步骤..."
STEP_START_APP=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"启动应用\", \"step_type\": \"navigate\", \"module_id\": $MODULE_LOGIN_ID, \"description\": \"启动被测试应用\"}" \
  | jq -r '.data.id')

STEP_NAVIGATE_LOGIN=$(curl -s -X POST "$BASE_URL/api/steps" \
  -H "Content-Type: application/json" \
  -d "{\"step_name\": \"导航到登录页面\", \"step_type\": \"navigate\", \"module_id\": $MODULE_LOGIN_ID, \"description\": \"导航到登录页面\"}" \
  | jq -r '.data.id')

echo -e "${GREEN}✅ 基础步骤创建完成${NC}"

# 4. 创建测试用例
echo -e "\n${YELLOW}4. 创建测试用例...${NC}"

echo -e "  📝 创建登录测试用例..."
CASE_LOGIN=$(curl -s -X POST "$BASE_URL/api/test-cases" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_name\": \"用户登录测试\",
    \"module_id\": $MODULE_LOGIN_ID,
    \"description\": \"测试用户登录功能\",
    \"steps\": [
      {\"step_id\": $STEP_INPUT_USER, \"parameters\": \"admin\"},
      {\"step_id\": $STEP_INPUT_PASS, \"parameters\": \"password123\"},
      {\"step_id\": $STEP_CLICK_LOGIN, \"parameters\": null}
    ]
  }" | jq -r '.data.id')

echo -e "  📝 创建菜单导航测试用例..."
CASE_MENU_NAV=$(curl -s -X POST "$BASE_URL/api/test-cases" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_name\": \"菜单导航测试\",
    \"module_id\": $MODULE_MENU_ID,
    \"description\": \"测试主菜单导航功能\",
    \"steps\": [
      {\"step_id\": $STEP_CLICK_RECORD, \"parameters\": null},
      {\"step_id\": $STEP_CLICK_DEVICE, \"parameters\": null},
      {\"step_id\": $STEP_CLICK_SETTINGS, \"parameters\": null}
    ]
  }" | jq -r '.data.id')

echo -e "  📝 创建网络设置测试用例..."
CASE_NETWORK_SET=$(curl -s -X POST "$BASE_URL/api/test-cases" \
  -H "Content-Type: application/json" \
  -d "{
    \"case_name\": \"网络设置测试\",
    \"module_id\": $MODULE_SETTINGS_ID,
    \"description\": \"测试网络设置功能\",
    \"steps\": [
      {\"step_id\": $STEP_CLICK_NETWORK, \"parameters\": null},
      {\"step_id\": $STEP_SAVE_SETTINGS, \"parameters\": null}
    ]
  }" | jq -r '.data.id')

echo -e "${GREEN}✅ 测试用例创建完成 (登录:$CASE_LOGIN, 菜单:$CASE_MENU_NAV, 设置:$CASE_NETWORK_SET)${NC}"

# 5. 创建设备
echo -e "\n${YELLOW}5. 创建自动化设备...${NC}"

echo -e "  🖥️  创建测试设备..."
DEVICE_ID=$(curl -s -X POST "$BASE_URL/api/devices" \
  -H "Content-Type: application/json" \
  -d '{"name": "测试设备01", "description": "主要测试设备", "ip_address": "192.168.1.100", "port": 8080}' \
  | jq -r '.data.id')

echo -e "  📱 创建设备型号..."
DEVICE_MODEL_ID=$(curl -s -X POST "$BASE_URL/api/test_strategy/device_models" \
  -H "Content-Type: application/json" \
  -d '{"model_name": "NVR-8000", "host": "192.168.1.100", "port": 22, "username": "admin", "password": "password123", "timeout": 10, "mac_address": "AA:BB:CC:DD:EE:FF", "vuid": "device001", "description": "8路网络硬盘录像机"}' \
  | jq -r '.data.id')

echo -e "${GREEN}✅ 设备创建完成 (设备:$DEVICE_ID, 型号:$DEVICE_MODEL_ID)${NC}"

# 6. 创建测试套件
echo -e "\n${YELLOW}6. 创建测试套件...${NC}"

echo -e "  📦 创建综合功能测试套件..."
SUITE_ID=$(curl -s -X POST "$BASE_URL/api/test-suites" \
  -H "Content-Type: application/json" \
  -d "{
    \"suite_name\": \"NVR综合功能测试套件\",
    \"description\": \"包含登录、菜单导航、设置等核心功能测试\",
    \"run_times\": 1,
    \"suite_pre_steps\": [{\"id\": $STEP_START_APP, \"type\": \"base\"}],
    \"case_pre_steps\": [{\"id\": $STEP_NAVIGATE_LOGIN, \"type\": \"base\"}],
    \"cases\": [
      {\"id\": $CASE_LOGIN},
      {\"id\": $CASE_MENU_NAV},
      {\"id\": $CASE_NETWORK_SET}
    ]
  }" | jq -r '.data.id')

echo -e "${GREEN}✅ 测试套件创建完成 (套件ID:$SUITE_ID)${NC}"

# 7. 创建测试策略
echo -e "\n${YELLOW}7. 创建测试策略...${NC}"

echo -e "  📅 创建每日测试策略..."
STRATEGY_ID=$(curl -s -X POST "$BASE_URL/api/test_strategy/strategies" \
  -H "Content-Type: application/json" \
  -d "{
    \"device_model_id\": $DEVICE_MODEL_ID,
    \"name\": \"NVR每日功能测试\",
    \"description\": \"每天定时执行NVR核心功能测试\",
    \"daily_plans\": [
      {\"weekday\": 1, \"execution_time\": \"09:00:00\", \"test_suite_id\": $SUITE_ID},
      {\"weekday\": 3, \"execution_time\": \"14:00:00\", \"test_suite_id\": $SUITE_ID},
      {\"weekday\": 5, \"execution_time\": \"18:00:00\", \"test_suite_id\": $SUITE_ID}
    ]
  }" | jq -r '.data.id')

echo -e "  ✅ 启用测试策略..."
curl -s -X PUT "$BASE_URL/api/test_strategy/strategies/$STRATEGY_ID/status" \
  -H "Content-Type: application/json" \
  -d '{"is_active": true}' > /dev/null

echo -e "${GREEN}✅ 测试策略创建完成 (策略ID:$STRATEGY_ID)${NC}"

# 8. 启动监控
echo -e "\n${YELLOW}8. 启动监控...${NC}"
curl -s -X POST "$BASE_URL/api/test_strategy/monitor/start" \
  -H "Content-Type: application/json" \
  -d "{\"strategy_ids\": [$STRATEGY_ID]}" > /dev/null

echo -e "${GREEN}✅ 监控启动成功${NC}"

# 显示创建结果
echo -e "\n${BLUE}========================================${NC}"
echo -e "${GREEN}🎉 演示数据创建完成！${NC}"
echo -e "${BLUE}========================================${NC}"

echo -e "\n${YELLOW}📊 创建的资源概览:${NC}"
echo -e "  📁 功能模块: 3个 (登录、主菜单、设置)"
echo -e "  📍 控件坐标: 8个"
echo -e "  🔧 基础步骤: 10个"
echo -e "  📝 测试用例: 3个"
echo -e "  🖥️  设备信息: 2个"
echo -e "  📦 测试套件: 1个"
echo -e "  📅 测试策略: 1个"

echo -e "\n${YELLOW}🔍 快速查看命令:${NC}"
echo -e "  查看所有模块: ${BLUE}curl $BASE_URL/api/module${NC}"
echo -e "  查看测试套件: ${BLUE}curl $BASE_URL/api/test-suites/$SUITE_ID${NC}"
echo -e "  查看监控状态: ${BLUE}curl $BASE_URL/api/test_strategy/monitor/status${NC}"
echo -e "  导出套件数据: ${BLUE}curl $BASE_URL/api/data/export/$SUITE_ID${NC}"

echo -e "\n${YELLOW}📅 执行计划:${NC}"
echo -e "  🕘 周一 09:00 - 执行NVR综合功能测试"
echo -e "  🕐 周三 14:00 - 执行NVR综合功能测试"
echo -e "  🕔 周五 18:00 - 执行NVR综合功能测试"

echo -e "\n${GREEN}✨ 演示数据已准备就绪，可以开始体验VEPAI平台！${NC}" 